package com.example.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;

import com.example.entity.File;
import com.example.entity.UserAction;
import com.example.service.FileService;
import com.example.service.LogService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-05-31
 */
@RestController
@RequestMapping("//file")
public class FileController {
    public static final ConcurrentHashMap<String, File> MAP = new ConcurrentHashMap<>();

    @Resource
    private FileService fileService;
    @Resource
    private LogService logService;
    @Resource
    private HttpServletRequest request;
    @PostMapping
    public Result<?> save(@RequestBody File file) {
        file.setUploadtime(DateUtil.formatDateTime(new Date()));
        logService.log(StrUtil.format("新增文件：{} ",file.getFilename()));

        return Result.success(fileService.save(file));
    }
    @GetMapping("/download")
    public List<File> querylist1(){
        return fileService.getlist1();
    }
    @GetMapping("/collect")
    public List<File> querylist2(){
        return fileService.getlist2();
    }
    @PutMapping
    public Result<?> update(@RequestBody File file) {
        file.setUpdatetime(DateUtil.formatDateTime(new Date()));
        logService.log(StrUtil.format("更新文件：{} ", file.getFilename()));
        return Result.success(fileService.updateById(file));
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        File file = fileService.getById(id);
        logService.log(StrUtil.format("删除文件{} ",file.getFilename()));
        fileService.removeById(id);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result<File> findById(@PathVariable Long id) {
        return Result.success(fileService.getById(id));
    }

    @GetMapping
    public Result<List<File>> findAll() {
        return Result.success(fileService.list());
    }
    @GetMapping("/page")
public Result<IPage<File>> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                          @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                          @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
    return Result.success(fileService.page(new Page<>(pageNum, pageSize), Wrappers.<File>lambdaQuery().like(File::getFilename, name)));
}
    @GetMapping("/collect/page")
    public Result<IPage<File>> findPage1(@RequestParam(required = false, defaultValue = "") String name,
                                        @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(fileService.page(new Page<>(pageNum, pageSize), Wrappers.<File>lambdaQuery().like(File::getFilename, name)));
    }
    @GetMapping("/download/page")
    public Result<IPage<File>> findPage2(@RequestParam(required = false, defaultValue = "") String name,
                                         @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                         @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(fileService.page(new Page<>(pageNum, pageSize), Wrappers.<File>lambdaQuery().like(File::getFilename, name)));
    }
}

